package com.ruoyi.mobile.project.sms.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.ruoyi.db.sms.domain.SmsGroupon;
import com.ruoyi.db.sms.domain.query.SmsGrouponQueryVo;
import com.ruoyi.db.sms.mapper.SmsGrouponMapper;
import com.ruoyi.mobile.project.utils.GrouponConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WxSmsGrouponService
{
    @Autowired
    private SmsGrouponMapper grouponMapper;

    /**
     * 获取用户发起的团购记录
     *彭畅
     * @param userId
     * @return
     */
    public List<SmsGroupon> queryMyGroupon(Long userId)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setUserId(userId);
        grouponQueryVo.setCreatorUserId(userId);
        grouponQueryVo.setOfferedGrouponId((long)0);
        grouponQueryVo.setStatus(GrouponConstant.STATUS_NONE); // xml设置不等于0
        return grouponMapper.queryMyGroupon(grouponQueryVo);
    }

    /**x
     * 获取用户参与的团购记录
     *彭畅
     * @param userId
     * @return
     */
    public List<SmsGroupon> queryMyJoinGroupon(Long userId)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setUserId(userId);
        grouponQueryVo.setOfferedGrouponId((long)0);  // xml设置不等于0
        grouponQueryVo.setStatus(GrouponConstant.STATUS_NONE);  // xml设置不等于0
        return grouponMapper.queryMyJoinGroupon(grouponQueryVo);
    }

    /**
     * 根据OrderId查询团购记录
     * @author 严方亚
     * @date 2020-05-12
     * @param orderId
     * @return
     */
    public List<SmsGroupon> queryByOrderId(Long orderId)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setOrderId(orderId);
        return grouponMapper.selectSmsGrouponList(grouponQueryVo);
    }

    /**
     * 获取某个团购活动参与的记录
     * @author 严方亚
     * @date 2020-05-17
     * @param id
     * @return
     */
    public List<SmsGroupon> queryJoinRecord(Long id)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setOfferedGrouponId(id);
        grouponQueryVo.setStatus(GrouponConstant.STATUS_NONE);
        return grouponMapper.queryJoinRecord(grouponQueryVo);
    }

    /**
     * 根据ID查询记录
     * @author 严方亚
     * @date 2020-05-17
     * @param id
     * @return
     */
    public SmsGroupon queryById(Long id)
    {
        return grouponMapper.selectSmsGrouponById(id);
    }

    /**
     * 根据ID查询记录
     *
     * @param userId
     * @param id
     * @return
     */
    public SmsGroupon queryById(Long userId, Long id)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setUserId(userId);
        grouponQueryVo.setId(id);
        return grouponMapper.selectSmsGrouponByIdAUserId(grouponQueryVo);
    }

    /**
     * 返回某个发起的团购参与人数
     * @author 严方亚
     * @date 2020-05-22
     * @param grouponId
     * @return
     */
    public int countGroupon(Long grouponId)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setOfferedGrouponId(grouponId); // 参团团购活动ID
        grouponQueryVo.setStatus(GrouponConstant.STATUS_NONE);
        return grouponMapper.countGroupon(grouponQueryVo);
    }

    /**
     * 判断该团购活动用户是否已加入
     * @author 严方亚
     * @date 2020-05-22
     * @param userId
     * @param grouponId
     * @return
     */
    public boolean hasJoin(Long userId, Long grouponId)
    {
        SmsGrouponQueryVo grouponQueryVo = new SmsGrouponQueryVo();
        grouponQueryVo.setUserId(userId);
        grouponQueryVo.setOfferedGrouponId(grouponId);
        grouponQueryVo.setStatus(GrouponConstant.STATUS_NONE);
        return  grouponMapper.countGroupon(grouponQueryVo) != 0;
    }

    /**
     * 团购更新
     *
     * @author 严方亚
     * @date 2020-05-17
     * @param groupon
     */
    public int updateById(SmsGroupon groupon)
    {
        groupon.setUpdateTime(LocalDateTime.now());
        return grouponMapper.updateSmsGroupon(groupon);
    }

    /**
     * 创建或参与一个团购
     * @author 严方亚
     * @date 2020-05-22
     * @param groupon
     * @return
     */
    public int createGroupon(SmsGroupon groupon)
    {
        groupon.setAddTime(LocalDateTime.now());
        groupon.setUpdateTime(LocalDateTime.now());
        return grouponMapper.insertSmsGroupon(groupon);
    }

    /**
     * 查询所有发起的团购记录
     *
     * @param rulesId
     * @param page
     * @param size
     * @param sort
     * @param order
     * @return
     */
    public List<SmsGroupon> querySelective(String rulesId, Integer page, Integer size, String sort, String order)
    {
        return null;
    }

    public List<SmsGroupon> queryByRuleId(int grouponRuleId)
    {
        return null;
    }
}
